home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Extending / c_files / Sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-01  |  1.0 KB  |  48 lines

  1.  
  2. #include "mm_jsapi.h"
  3.  
  4.  
  5. /* Every implementation of a Javascript function must have this signature */
  6. JSBool
  7. computeSum(JSContext *cx, JSObject *obj, unsigned int argc, 
  8.     jsval *argv, jsval *rval)
  9. {
  10.     long a, b, sum;
  11.  
  12.     /* Make sure the right number of arguments were passed in */
  13.     if (argc != 2)
  14.         return JS_FALSE;
  15.  
  16.     /* Convert the two arguments from jsvals to longs */
  17.     if (JS_ValueToInteger(cx, argv[0], &a) == JS_FALSE ||
  18.         JS_ValueToInteger(cx, argv[1], &b) == JS_FALSE)
  19.         return JS_FALSE;
  20.  
  21.     /* Perform the actual work */
  22.     sum = a + b;
  23.  
  24.     /* Package the return value as a jsval */
  25.     *rval = JS_IntegerToValue(sum);
  26.  
  27.     /* Indicate success */
  28.     return JS_TRUE;
  29. }
  30.  
  31.  
  32.  
  33. /* MM_STATE is a macro that expands to some definitions that are
  34.  * needed in order interact with Dreamweaver.  This macro must be
  35.  * defined exactly once in your library */
  36. MM_STATE
  37.  
  38.  
  39.  
  40. /* Dreamweaver calls MM_Init when your library is loaded. */
  41. void
  42. MM_Init()
  43. {
  44.     /* Declare the Javascript function, giving it a name that's
  45.      * likely to be unique */
  46.     JS_DefineFunction("computeSum", computeSum, 2);
  47. }
  48.